<!-- 添加‘富文本’详情-->
<template>
  <ele-modal
    width="680px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="title"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15" class="flex-columns">
        <el-col style="padding: 0 10px">
          <!--v-if防止tinymce在弹框创建前创建-->
          <tinymce-editor
            v-if="visible"
            ref="editor"
            v-model="form.logUpdate"
            :init="option"
            :disabled="false"
          />
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>


<script>
  import TinymceEditor from '@/components/TinymceEditor/index.vue';
  import {mealAdd, mealUpdate} from "@/api/system/log";
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: {TinymceEditor},
    name: "add-detail",
    props: {
      // 弹窗是否打开
      visible: Boolean
    },
    data() {
      const defaultForm = {
      };
      return {
        title:'',
        option: {
          height: 520,
          // 自定义文件上传(这里使用把选择的文件转成 blob 演示)
          file_picker_callback: (callback, _value, meta) => {
            const input = document.createElement('input');
            input.setAttribute('type', 'file');
            // 设定文件可选类型
            if (meta.filetype === 'image') {
              input.setAttribute('accept', 'image/*');
            } else if (meta.filetype === 'media') {
              input.setAttribute('accept', 'video/*');
            }
            input.onchange = () => {
              const file = input.files?.[0];
              if (!file) {
                return;
              }
              if (meta.filetype === 'media') {
                if (!file.type.startsWith('video/')) {
                  this.$refs.editor.alert({ content: '只能选择视频文件' });
                  return;
                }
              }
              if (file.size / 1024 / 1024 > 20) {
                this.$refs.editor.alert({ content: '大小不能超过 20MB' });
                return;
              }
              const reader = new FileReader();
              reader.onload = (e) => {
                if (e.target?.result != null) {
                  const blob = new Blob([e.target.result], { type: file.type });
                  callback(URL.createObjectURL(blob));
                }
              };
              reader.readAsArrayBuffer(file);
            };
            input.click();
          }
        },
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        types:-1
      }
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      // 内容和类型
      show(details, type){
        this.title=''
        this.$set(this.form,'logUpdate',null)
        if(details){
          this.form.logUpdate=details
        }
        switch(type)
        {
          case 1:
            this.title=details ? '修改矩阵详情' : '添加矩阵详情'
            break;
          case 2:
            this.title=details ? '修改视频剪辑详情' : '添加视频剪辑详情'
            break;
          case 3:
            this.title=details ? '修改云闪推详情' : '添加云闪推详情'
            break;
          case 4:
            this.title=details ? '修改现场大屏详情' : '添加现场大屏详情'
            break;
          case 5:
            this.title=details ? '修改抖支付详情' : '添加抖支付详情'
            break;
          case 6:
            this.title=details ? '修改数字人详情' : '添加数字人详情'
            break;
          default:
            console.log("进入default")
            break;
        }
        this.types=type
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          this.updateVisible(false)
          this.$emit('done',this.form.logUpdate,this.types)
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
        this.loading = false;
      }
    }
  }
</script>

<style scoped>

</style>
